package com.youngking.guns.modular.example.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.youngking.guns.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/importdd")
public class TestEayPoiController extends BaseController {
    @RequestMapping("/import")
    public Object importdd(HttpServletRequest request, HttpServletResponse response , Model model){
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            MultipartFile file = entity.getValue();
            try {
                InputStream oo = file.getInputStream();
                ImportParams params = new ImportParams();
                params.setTitleRows(0);
                params.setHeadRows(1);
                //params.setSheetNum(9);
                params.setNeedSave(true);
                long start = new Date().getTime();
                List<Map> list = ExcelImportUtil.importExcel(oo, Map.class,params);
                System.out.println(list.toString());
            }catch (Exception e){
                e.printStackTrace();
            }finally{
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }
}
